package sk.stuba.fiit.foo07.genex.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import sk.stuba.fiit.foo07.genex.common.SettingsHelper;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PropertiesDialog extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = 4005386130323361864L;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JTextField jTxtHeveaPath;
    private JTextField jTxtExportPath;
    private JButton jBtnHeveaPath;
    private JButton jBtnExportPath;
    private JButton jBtnOK;

    /**
     * Auto-generated main method to display this JDialog
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                PropertiesDialog inst = new PropertiesDialog(frame);
                inst.setVisible(true);
            }
        });
    }

    public PropertiesDialog(JFrame frame) {
        super(frame);
        initGUI();
    }

    private void initGUI() {

        try {
            {
                String hevea_path = SettingsHelper.getSetting("hevea_path");

                this.setTitle("Nastavenia");
                getContentPane().setLayout(null);
                this.setResizable(false);
                this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                this.setModalityType(ModalityType.DOCUMENT_MODAL);
                this.setLocationByPlatform(true);
                {
                    jLabel1 = new JLabel();
                    getContentPane().add(jLabel1);
                    jLabel1.setText("Cesta k programu Hevea");
                    jLabel1.setBounds(12, 12, 260, 14);
                    jLabel1.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtHeveaPath = new JTextField();
                    getContentPane().add(jTxtHeveaPath);
                    jTxtHeveaPath.setBounds(12, 32, 477, 21);
                    jTxtHeveaPath.setBackground(new java.awt.Color(255, 255,
                            240));
                    jTxtHeveaPath.setForeground(new java.awt.Color(0, 0, 255));
                    if (hevea_path != null)
                        jTxtHeveaPath.setText(hevea_path);

                }
                //-------------
                {
                    jLabel2 = new JLabel();
                    getContentPane().add(jLabel2);
                    jLabel2.setText("Adresar pre export testov do Latexu");
                    jLabel2.setBounds(12, 62, 280, 14);
                    jLabel2.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtExportPath = new JTextField();
                    getContentPane().add(jTxtExportPath);
                    jTxtExportPath.setBounds(12, 82, 477, 21);
                    jTxtExportPath.setBackground(new java.awt.Color(255, 255,
                            240));
                    jTxtExportPath.setForeground(new java.awt.Color(0, 0, 255));
                    try {
                        jTxtExportPath.setText(SettingsHelper
                                .getSetting("export_test_path"));
                    } catch (Exception e) {
                        jTxtExportPath.setText("");
                    }
                }
                {
                    jBtnExportPath = new JButton();
                    getContentPane().add(jBtnExportPath);
                    jBtnExportPath.setText("...");
                    jBtnExportPath.setBounds(495, 82, 34, 21);
                    jBtnExportPath.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnExportPathActionPerformed(evt);
                        }
                    });
                }
                //-------------
                {
                    jBtnHeveaPath = new JButton();
                    getContentPane().add(jBtnHeveaPath);
                    jBtnHeveaPath.setText("...");
                    jBtnHeveaPath.setBounds(495, 32, 34, 21);
                    jBtnHeveaPath.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnHeveaPathActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnOK = new JButton();
                    getContentPane().add(jBtnOK);
                    jBtnOK.setText("OK");
                    jBtnOK.setBounds(433, 304, 96, 21);
                    jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
                    jBtnOK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnOKActionPerformed(evt);
                        }
                    });
                }
            }
            this.setSize(551, 372);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jBtnHeveaPathActionPerformed(ActionEvent evt) {
        JFileChooser jfc = new JFileChooser(".");
        jfc.setFileFilter(new FileNameExtensionFilter("Spustiteľné súbory",
                "exe"));
        int state = jfc.showOpenDialog(this);
        if (state == JFileChooser.CANCEL_OPTION)
            return;

        File f = jfc.getSelectedFile();
        if (f == null)
            return;
        jTxtHeveaPath.setText(f.getAbsolutePath());
    }

    private void jBtnExportPathActionPerformed(ActionEvent evt) {
        JFileChooser jfc = new JFileChooser(".");
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int state = jfc.showOpenDialog(this);
        if (state == JFileChooser.CANCEL_OPTION)
            return;

        File f = jfc.getSelectedFile();
        if (f == null)
            return;
        jTxtExportPath.setText(f.getAbsolutePath());
    }

    private void jBtnOKActionPerformed(ActionEvent evt) {
        setVisible(false);
        try {
            SettingsHelper.setSetting("hevea_path", jTxtHeveaPath.getText());
            SettingsHelper.setSetting("export_test_path", jTxtExportPath
                    .getText());
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getLocalizedMessage());
        }
    }

}
